﻿package com.badibadi.logic;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.badibadi.mytools.BitmapHelp;
import com.badibadi.mytools.ImageTools;
import com.badibadi.uniclubber.R;
import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.util.LruCache;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;

//ImgCallBack
public class ImgsAdapter extends BaseAdapter {

	private static final String TAG = "ImgsAdapter";
	Context context;
	List<String> data;
	public Bitmap bitmaps[];
	Util util;
	OnItemClickClass onItemClickClass;
	private int index = -1;

	// List<View> holderlist;

	BitmapUtils bitmapUtils;

	OnImgsGridViewScrollListener onImgsGridViewScrollListener;// 自己加入的GridView

	LayoutInflater inflater;

	/**
	 * 存放选中的标签的position
	 */
	private Map<Integer, String> TitleMap;//根据它来记录是否选中
	
	
	public ImgsAdapter(Context context, List<String> data,
			OnItemClickClass onItemClickClass) {
		this.context = context;
		this.data = data;
		this.onItemClickClass = onItemClickClass;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		bitmaps = new Bitmap[data.size()];
		util = new Util(context);
		TitleMap=new HashMap<Integer, String>();
		// holderlist = new ArrayList<View>();
		// bitmapUtils=BitmapHelp.getBitmapUtils(context);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int arg0) {
		return data.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	// public void setIndex(int index) {
	// this.index = index;
	// }

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		Holder holder;
		// if (arg0 != index && arg0 > index) {
		if (arg1 == null) {
			index = arg0;
			arg1 = inflater.inflate(R.layout.img_sitem, null);
			// arg1 = LayoutInflater.from(context).inflate(R.layout.img_sitem,
			// null);
			holder = new Holder();
			holder.imageView = (ImageView) arg1.findViewById(R.id.imageView1);
			holder.checkBox = (CheckBox) arg1.findViewById(R.id.checkBox1);
			arg1.setTag(holder);
			// holderlist.add(arg1);
		} else {
			// holder = (Holder) holderlist.get(arg0).getTag();
			holder = (Holder) arg1.getTag();
			// arg1 = holderlist.get(arg0);
		}
		// if (bitmaps[arg0] == null) {
		// util.imgExcute(holder.imageView, new ImgClallBackLisner(arg0),
		// data.get(arg0));
		// } else {
		if(TitleMap.containsKey(arg0)==true)
		{
			holder.checkBox.setChecked(true);
		}
		else
		{
			holder.checkBox.setChecked(false);
		}
		
		String path = data.get(arg0);
		
		
		// int degree = ImageTools.readPictureDegree(path);
		// BitmapFactory.Options opts = new BitmapFactory.Options();//
		// 获取缩略图显示到屏幕上
		// opts.inSampleSize = 2;
		// opts.inJustDecodeBounds = true;
		// BitmapFactory.decodeFile(data.get(arg0), opts);
		// // 计算图片缩放比例
		// final int minSideLength = Math.min(100, 100);
		// opts.inSampleSize = ImageTools.computeSampleSize(opts,
		// minSideLength, 100 * 100);
		// opts.inJustDecodeBounds = false;
		// opts.inInputShareable = true;
		// opts.inPurgeable = true;
		//
		// Bitmap cbitmap = BitmapFactory.decodeFile(path, opts);
		// /**
		// * 把图片旋转为正的方向
		// */
		// Bitmap newbitmap = ImageTools.rotaingImageView(degree, cbitmap);
		// holder.imageView.setImageBitmap(newbitmap);// bitmaps[arg0]
		if (onImgsGridViewScrollListener != null) {
			onImgsGridViewScrollListener.ImgsGridView(holder.imageView, path);
			// }
			// new BitmapHelp().displayImage(context, holder.imageView, path);

		}
		arg1.setOnClickListener(new OnPhotoClick(arg0, holder.checkBox));
		return arg1;
	}

	class Holder {
		ImageView imageView;
		CheckBox checkBox;
	}

	public class ImgClallBackLisner implements ImgCallBack {
		int num;

		public ImgClallBackLisner(int num) {
			this.num = num;
		}

		@Override
		public void resultImgCall(ImageView imageView, Bitmap bitmap,
				String path2) {
			bitmaps[num] = bitmap;

			String path = data.get(num);
			// int degree = ImageTools.readPictureDegree(path);
			// BitmapFactory.Options opts = new BitmapFactory.Options();//
			// 获取缩略图显示到屏幕上
			// opts.inSampleSize = 2;
			// opts.inJustDecodeBounds = true;
			// BitmapFactory.decodeFile(data.get(num), opts);
			// // 计算图片缩放比例
			// final int minSideLength = Math.min(100, 100);
			// opts.inSampleSize = ImageTools.computeSampleSize(opts,
			// minSideLength, 100 * 100);
			// opts.inJustDecodeBounds = false;
			// opts.inInputShareable = true;
			// opts.inPurgeable = true;
			//
			// Bitmap cbitmap = BitmapFactory.decodeFile(path, opts);
			// /**
			// * 把图片旋转为正的方向
			// */
			// Bitmap newbitmap = ImageTools.rotaingImageView(degree, cbitmap);
			// imageView.setImageBitmap(newbitmap);
			if (onImgsGridViewScrollListener != null) {
				onImgsGridViewScrollListener.ImgsGridView(imageView, path);
			}

			// new BitmapHelp().displayImage(context, imageView, path);
		}
	}

	public interface OnItemClickClass {
		public void OnItemClick(View v, int Position, CheckBox checkBox);
	}

	/**
	 * 设置gridView滚动（自己加的接口）
	 * 
	 * @param onImgsGridViewScrollListener
	 */
	public void setOnImgsGridViewScrollListener(
			OnImgsGridViewScrollListener onImgsGridViewScrollListener) {
		this.onImgsGridViewScrollListener = onImgsGridViewScrollListener;
	}

	public interface OnImgsGridViewScrollListener {
		public void ImgsGridView(ImageView iv, String path);
	}

	class OnPhotoClick implements OnClickListener {
		int position;
		CheckBox checkBox;

		public OnPhotoClick(int position, CheckBox checkBox) {
			this.position = position;
			this.checkBox = checkBox;
		}

		@Override
		public void onClick(View v) {
			if (data != null && onItemClickClass != null) {
				onItemClickClass.OnItemClick(v, position, checkBox);
			}
		}
	}

	public Map<Integer, String> getTitleMap() {
		return TitleMap;
	}

	public void setTitleMap(Map<Integer, String> titleMap) {
		TitleMap = titleMap;
	}
	
	

}
